﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class FAQsBLL
  {
    //Get all

    public List<FAQ> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.FAQs.ToList();
      }
    }

    public List<FAQ> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.FAQs.Where(p => p.isEnable == isEnable).ToList();
      }
    }

    public List<FAQ> GetByIsAnswer(bool isAnswer)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.FAQs.Where(p => p.isAnswer == isAnswer).OrderByDescending(p =>p.Sort).ToList();
      }
    }

    public List<FAQ> GetByIsAnswer(bool isAnswer,bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.FAQs.Where(p => p.isAnswer == isAnswer && p.isEnable == isEnable).OrderByDescending(p => p.Sort).ToList();
      }
    }

    //Get by Id

    public FAQ GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.FAQs.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public FAQ GetById(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.FAQs.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    //Enable

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        FAQ obj = data.FAQs.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;
        
        data.SubmitChanges();
      }
    }

    public void Enable(int id,bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        FAQ obj = data.FAQs.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }

    //Get max Sort

    public int GetMaxSort(bool isAnswer)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        FAQ obj = data.FAQs.Where(p => p.isAnswer == isAnswer).OrderByDescending(p => p.Sort).FirstOrDefault();

        if (obj == null) return 1;
        else return (obj.Sort + 1);
      }
    }

    //Swap sort

    public void SwapSort(int idFirst, int idSecond)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        FAQ objFirst = data.FAQs.Where(p => p.Id == idFirst).FirstOrDefault();
        FAQ objSecond = data.FAQs.Where(p => p.Id == idSecond).FirstOrDefault();

        int tg = objFirst.Sort;
        objFirst.Sort = objSecond.Sort;
        objSecond.Sort = tg;

        data.SubmitChanges();
      }
    }
    //Add

    public void Add(FAQ obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.FAQs.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    //Update

    public void Update(FAQ obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        FAQ faq = data.FAQs.Where(p => p.Id == obj.Id).FirstOrDefault();
        faq.Answer = obj.Answer;
        faq.DateSend = obj.DateSend;
        faq.isEnable = obj.isEnable;
        faq.Name = obj.Name;
        faq.Question = obj.Question;
        faq.Title = obj.Title;
        faq.isAnswer = obj.isAnswer;
        faq.Sort = obj.Sort;

        data.SubmitChanges();
      }
    }

    //Delete

    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        FAQ obj = data.FAQs.Where(p => p.Id == id).FirstOrDefault();
        data.FAQs.DeleteOnSubmit(obj);
        data.SubmitChanges();
      }
    }
  }
}
